package com.yami.shop.app.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.PinyinUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yami.shop.app.utils.ExcelUtil;
import com.yami.shop.app.utils.HttpResult;
import com.yami.shop.app.dto.UserDto;
import com.yami.shop.bean.cashregisterapp.AppCashRegisterSystemUser;
import com.yami.shop.bean.model.*;
import com.yami.shop.service.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.undertow.util.ConnectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.yami.shop.app.dto.JiangziweidaoProdDto;
import com.yami.shop.bean.model.Product;

@RestController
@RequestMapping("/app/excel")
public class ExcelController {

    @Autowired
    private UserTemporaryService userTemporaryService;
    @Autowired
    private UserService userService;
    @Autowired
    private ProductService productService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private OldUserService oldUserService;

    /**
     * 获取店铺信息
     * @param
     * @return
     */
    @PostMapping("/excelForUserTemp")
    public HttpResult excelForUserTemp(){

        return HttpResult.success();
    }

    private ExcelUtil excelUtil;

    @PostMapping(value="/uploadProdExcel")
    public String uploadProdExcel(@RequestParam(value="uploadFile", required = false) MultipartFile file){
        List<Product> productList = excelUtil.readExcel("",Product.class,file);
        for(Product product:productList){

        }
        return null;
    }

    @PostMapping(value="/jiangziweidaoProd")
    public String jiangziweidaoProd(@RequestParam(value="uploadFile", required = false) MultipartFile file){
        List<JiangziweidaoProdDto> jiangziweidaoProdDtos = excelUtil.readExcel("",JiangziweidaoProdDto.class,file);
        List<Product> productList = new ArrayList<>();
        for(JiangziweidaoProdDto jiangziweidaoProdDto : jiangziweidaoProdDtos){
            Product product = new Product();
            product.setShopId(new Long(116));
            product.setProdName(jiangziweidaoProdDto.getProdName());
            product.setBarCode(jiangziweidaoProdDto.getBarCode());
            product.setPrice(jiangziweidaoProdDto.getPrice().doubleValue());
            product.setUnit(jiangziweidaoProdDto.getUtil());
            if(jiangziweidaoProdDto.getSecondaryClassification().equals("小商品")){
                product.setCategoryId(new Long(575));
            }else if(jiangziweidaoProdDto.getSecondaryClassification().equals("常温蛋糕")){
                product.setCategoryId(new Long(567));
            }else if(jiangziweidaoProdDto.getSecondaryClassification().equals("干点类")){
                product.setCategoryId(new Long(568));
            }
            else if(jiangziweidaoProdDto.getSecondaryClassification().equals("月饼")){
                product.setCategoryId(new Long(569));
            }
            else if(jiangziweidaoProdDto.getSecondaryClassification().equals("生日蛋糕类")){
                product.setCategoryId(new Long(570));
            }
            else if(jiangziweidaoProdDto.getSecondaryClassification().equals("粽子")){
                product.setCategoryId(new Long(571));
            }else if(jiangziweidaoProdDto.getSecondaryClassification().equals("西点")){
                product.setCategoryId(new Long(572));
            }
            else if(jiangziweidaoProdDto.getSecondaryClassification().equals("面包类")){
                product.setCategoryId(new Long(573));
            }else if(jiangziweidaoProdDto.getSecondaryClassification().equals("饮品")){
                product.setCategoryId(new Long(574));
            }
            product.setCreateTime(new Date());
            product.setStatus(1);
            product.setTotalStocks(1000);
            product.setPutawayTime(new Date());
            product.setSaleType(2);
            product.setVipDiscount(88);
            product.setProdNamePy(getPy(product.getProdName()));
            productService.save(product);
            System.out.println("产品id   "+product.getProdId());

            Sku sku = new Sku();
            sku.setProdId(product.getProdId());
            sku.setOriPrice(product.getOriPrice());
            sku.setPrice(product.getPrice());
            sku.setStocks(product.getTotalStocks());
            sku.setActualStocks(product.getTotalStocks());
            sku.setProdName(product.getProdName());
            sku.setModelId(product.getBarCode());
            sku.setUpdateTime(new Date());
            sku.setRecTime(new Date());
            skuService.save(sku);
            //break;
            //productList.add(product);
        }
        System.out.println("酱子味道商品数量"+productList.size());
        return null;
    }
//65e4068f-d9e7-4ffd-aa9d-289f703da70c

    @PostMapping(value = "/uploadExcel")
    public String uploadExcel(@RequestParam(value="uploadFile", required = false) MultipartFile file) throws Exception {

        List<UserDto> dtoList = excelUtil.readExcel("", UserDto.class,file);
        //List<String> mobiles=new ArrayList<>();
        List<UserTemporary> userTemporaries = new ArrayList<>();
        for(UserDto userDto : dtoList){
            String telRegex = "[1][3578]\\d{9}";
            if(userDto.getMobile().matches(telRegex)){
               /* List<User> users = userService.getUserByPhone(userDto.getMobile());
                if(CollectionUtils.isNotEmpty(users)){
                   mobiles.add(userDto.getMobile());
                }*/
               UserTemporary userTemporary  =new UserTemporary();
               userTemporary.setNickName(userDto.getName());
               userTemporary.setSex(userDto.getSex());
               userTemporary.setUserMobile(userDto.getMobile());
               SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
               if(userDto.getBirthDate()!=null){
                   userTemporary.setBirthDate(simpleDateFormat.format(userDto.getBirthDate()));
               }
               userTemporary.setStore(userDto.getStore());
               SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("YYYY-MM-dd HH:MM:SS");
               if(userDto.getCreateTime()!=null){
                   userTemporary.setUserRegtime( new Date(simpleDateFormat1.format(userDto.getCreateTime())));
               }else{
                   userTemporary.setUserRegtime(new Date());
               }
               if(userDto.getAmount()!=null){
                   userTemporary.setAccount(userDto.getAmount().doubleValue());
               }
               if(userDto.getRecharge()!=null){
                   userTemporary.setRecharge(userDto.getRecharge());
               }
               if(userDto.getGive()!=null){
                   userTemporary.setGive(userDto.getGive());
               }
                userTemporary.setUserId(IdUtil.simpleUUID());
                userTemporary.setModifyTime(new Date());
                userTemporaries.add(userTemporary);

                //根据电话号码查询是否有对应的客户
                User user = userService.selectUserByPhoneOne(userDto.getMobile());
                if(user!=null){
                    //查询表中有没有对应的数据(老数据表中)
                    String userId = user.getUserId();
                    OldUser oldUser = oldUserService.getOldUserByShopId(userId,116l);
                    if(oldUser==null){
                        OldUser oldUser1 = new OldUser();
                        oldUser1.setUserId(userId);
                        oldUser1.setShopId(116l);
                        oldUser1.setAccount(userDto.getAmount());
                        oldUserService.saveOrUpdate(oldUser1);
                    }else{

                    }
                }else{
                    //新建用户表
                    User newUser = new User();
                    if(userDto.getSex().equals("女")){
                        newUser.setSex("F");
                    }else{
                        newUser.setSex("M");
                    }
                    newUser.setUserId(IdUtil.simpleUUID());
                    newUser.setNickName(userDto.getName());
                    newUser.setUserMobile(userDto.getMobile());
                    if(userDto.getBirthDate()!=null){
                        newUser.setBirthDate(simpleDateFormat.format(userDto.getBirthDate()));
                    }
                    newUser.setModifyTime(new Date());
                    newUser.setUserRegtime(new Date());
                    userService.saveOrUpdate(newUser);

                    //新增oldUser账户表
                    OldUser oldUser = new OldUser();
                    oldUser.setUserId(newUser.getUserId());
                    oldUser.setAccount(userDto.getAmount());
                    oldUser.setShopId(116l);
                    //newUser.setImportNumber("0525-116");
                    oldUserService.saveOrUpdate(oldUser);
                }
            }

        }
        System.out.println("导入的客户数量"+userTemporaries.size());
        userTemporaryService.saveBatch(userTemporaries);
        //System.out.println("重复的电话号码"+mobiles);
        return "导入成功";
    }
    public String getPy(String prodNamePy){
        String convert = "";
        for(int i=0;i<prodNamePy.length();i++){
            char word = prodNamePy.charAt(i);
            if(String.valueOf(word).matches("[\\u4e00-\\u9fa5]")){
                char pinyinArray = PinyinUtil.getFirstLetter(word);
                convert += pinyinArray;
            }else{
                convert += word;
            }
        }
        return convert;
    }




}
